using UnityEngine;
using QFramework;
using System;
using System.Collections.Generic;
using ProjectSurvivor;
using Unity.VisualScripting.FullSerializer;

namespace QFrameworkSurvivor
{
    public partial class EnemyGenerator : ViewController
	{
        [SerializeField]
        public LevelConfig Config;
        private float mCurrentGenerateSeconds = 0;
        private float mCurrentWaveSeconds = 0;
        public static BindableProperty<int> EnemyCount = new BindableProperty<int>(defaultValue: 0);

        public Queue<EnemyWave> mEnemyWavesQueue = new Queue<EnemyWave>();
        public int WaveCount = 0;
        private int mTotalCount = 0;
        public bool Lastwave => WaveCount == mTotalCount;
        public EnemyWave Currentwave => mCurrentWave;
        private void Start()
        {
            foreach (var group in Config.EnemyWaveGroups)
            {
                foreach (var wave in group.Waves)
                {
                    mEnemyWavesQueue.Enqueue(wave);
                    mTotalCount++;
                }
            }
        }
        private EnemyWave mCurrentWave = null;
        private void Update()
        {
            if(mCurrentWave == null)
            {
                if (mEnemyWavesQueue.Count > 0)
                {
                    WaveCount++;
                    mCurrentWave = mEnemyWavesQueue.Dequeue();
                    mCurrentGenerateSeconds = 0;
                    mCurrentWaveSeconds = 0;
                }
            }
            if(mCurrentWave!=null)
            {
                mCurrentGenerateSeconds += Time.deltaTime;
                mCurrentWaveSeconds += Time.deltaTime;
                
                if (mCurrentGenerateSeconds >= mCurrentWave.GenerateDuration)
                {
                    mCurrentGenerateSeconds = 0;
                    var player = Player.Default;
                    if (player)
                    {
                        var xOry = RandomUtility.Choose(-1,1);
                        var pos=Vector2.zero;
                        if (xOry==-1)
                        {
                            pos.x=RandomUtility.Choose(CameraController.LBTran.position.x,CameraController.RTTran.position.x);
                            pos.y=UnityEngine.Random.Range(CameraController.LBTran.position.y,CameraController.RTTran.position.y);
                        }
                        else
                        {
                            pos.y = RandomUtility.Choose(CameraController.LBTran.position.y, CameraController.RTTran.position.y);
                            pos.x = UnityEngine.Random.Range(CameraController.LBTran.position.x, CameraController.RTTran.position.x);
                        }

                        mCurrentWave.EnemyPrefab.Instantiate()
                         .Self(self => 
                         {
                             var enemy=self.GetComponent<IEnemy>();
                             enemy.SetSpeedScale(mCurrentWave.SpeedScale);
                             enemy.SetHpScale(mCurrentWave.HpScale);
                         })
                         .Position(pos)
                         .Show();
                    }
                }
                if (mCurrentWaveSeconds >= mCurrentWave.Seconds)
                {
                    mCurrentWave = null;
                }
            }
        }
    }
}
